package com.iot.core.utils;

import com.iot.core.constant.ResponseEnum;

import java.util.HashMap;

public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {
		put("code", ResponseEnum.SUCCESS.getCode());
		put("msg", ResponseEnum.SUCCESS.getMsg());
		put("status", "success");
		put("data",null);
	}

	public static R error() {
		return error(ResponseEnum.FAILED.getCode(), ResponseEnum.FAILED.getMsg());
	}

	public static R error(String msg) {
		return error(ResponseEnum.FAILED.getCode(), msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R fromEnum(ResponseEnum responseEnum) {
		R r = new R();
		r.put("code", responseEnum.getCode());
		r.put("msg", responseEnum.getMsg());
		return r;
	}

	public static R fromEnum(ResponseEnum responseEnum,Object obj) {
		R r = new R();
		r.put("code", responseEnum.getCode());
		r.put("msg", responseEnum.getMsg());
		r.put("data", obj);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}

	public static R ok(Object result) {
		R r = new R();
		r.put("data",result);
		return r;
	}

	public static R ok(String msg,Object result) {
		R r = new R();
		r.put("msg", msg);
		r.put("data",result);
		return r;
	}

	public static R ok() {
		return new R();
	}

	public String getCode() {
		if (this.get("code") == null) {
			return null;
		}
		return this.get("code").toString();
	}

	public String getMsg() {
		if (this.get("msg") == null) {
			return null;
		}
		return this.get("msg").toString();
	}

	public String getResult() {
		if (this.get("data") == null) {
			return null;
		}
		return this.get("data").toString();
	}
//	@Override
//	public R put(String key, Object value) {
//		super.put(key, value);
//		return this;
//	}
}
